/**
 * app快捷方式持久化功能
 * @date 2023/9/27
 */
import type { StoreInterface } from "@/typings/store";
import type { DesktopInterface } from "@/typings/desktop";
import { allAppShortcutList } from "@/constants/app";
import emitter from "@utils/common/Mitt";
import { EmitNames } from "@/constants/common";

export const useAppStore = defineStore(
  "app-store",
  () => {
    const appShortcutMap = ref<StoreInterface.AppShortcutMap>({});
    const nowActiveApp = ref<DesktopInterface.AppShortcut>();

    /**
     * 初始化程序坞的数据
     */
    const initShortcutList = () => {
      if (Object.keys(appShortcutMap.value).length == 0) {
        allAppShortcutList.map(item => {
          appShortcutMap.value[item.key] = item;
        });
      }
    };

    /**
     * 打开app
     * @param app
     */
    const openApp = (app: DesktopInterface.AppShortcut) => {
      if (app.isLink) {
        if (app.linkType == "out") {
          window.open(app.link);
          return;
        }
      }
      const key = app.key;
      const targetApp = appShortcutMap.value[key];

      if (targetApp) {
        showThisApp(targetApp);
      }
    };

    /**
     * 显示当前app
     * @param app
     */
    const showThisApp = (app: DesktopInterface.AppShortcut) => {
      app.isHide = false;
      app.isTop = true;
      app.isKeepInDock = true;
      app.isRunning = true;
      let nowApp = nowActiveApp.value;
      if (nowApp) {
        if (nowApp.key != app.key) {
          nowApp.isTop = false;
          appShortcutMap.value[nowApp.key] = nowApp;
        }
      }
      nowActiveApp.value = app;
      emitter.emit(EmitNames.ChangeDesktopNav);
    };

    /**
     * 根据key显示当前app
     * @param key
     */
    const showThisAppByKey = (key: string) => {
      const targetApp = appShortcutMap.value[key];
      if (targetApp) {
        showThisApp(targetApp);
      }
    };

    /**
     * 关闭app
     * @param key
     */
    const closeApp = (key: string) => {
      let app = appShortcutMap.value[key];
      app.isTop = false;
      app.isHide = true;
      app.isRunning = false;
      app.isKeepInDock = false;
      appShortcutMap.value[key] = app;
      nowActiveApp.value = undefined;
      openPreviousApp();
    };

    /**
     * 打开上一次的应用
     */
    const openPreviousApp = () => {
      let keyList = Object.keys(appShortcutMap.value);
      let b = true;
      for (const key of keyList) {
        const app = appShortcutMap.value[key];
        if (app.isRunning && !app.isHide) {
          showThisAppByKey(key);
          b = false;
          break;
        }
      }
      if (b) emitter.emit(EmitNames.ChangeDesktopNav);
    };

    /**
     * 隐藏app应用
     * @param key
     */
    const hideApp = (key: string) => {
      let app = appShortcutMap.value[key];
      if (app) {
        app.isTop = false;
        app.isHide = true;
        appShortcutMap[key] = app;
        openPreviousApp();
      }
    };

    return {
      appShortcutMap,
      nowActiveApp,
      initShortcutList,
      openApp,
      showThisApp,
      showThisAppByKey,
      closeApp,
      hideApp,
    };
  },
  {
    persist: {
      storage: sessionStorage,
    },
  },
);
